package com.ruoyi.lvlian.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.nio.file.Files;
import java.nio.file.Paths;

public class HttpClientFromFile {

    public static void main(String[] args) {
        String url = "https://home.kaifangqian.com/resrun-paas/kaifangqian/openAPI/V2/contract/draft";


        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

            JsonFileReaderUtil jsonReader = new JsonFileReaderUtil();
            // 1. 读取JSON文件内容
            // 测试场景1：读取JSON文件为字符串
            String jsonInputString = jsonReader.readJsonFile("request.json");
            System.out.println("=== JSON文件内容（字符串格式）===");
            System.out.println(jsonInputString);



            // 2. 创建POST请求
            HttpPost post = new HttpPost(url);
            post.setHeader("Content-Type", "application/json");
            post.setHeader("Accept", "application/json");
            post.setEntity(new StringEntity(jsonInputString));

            // 3. 执行请求并获取响应
            HttpResponse response = httpClient.execute(post);
            String responseBody = EntityUtils.toString(response.getEntity());

            System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
            System.out.println("Response: " + responseBody);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}